import React, {useEffect, useRef, useState} from 'react';
import {Button, Col, Input, Row} from "antd";
import "video-react/dist/video-react.css";
import {
    Player,
} from 'video-react';
import {Upload} from "@arco-design/web-react";
import {mobileType, uploadUrl} from "../utils/tools";
import {getFile, getNewsFile, insertFile} from "../services/auth";
import axios from "axios";
import qs from "qs";



export default function VideoUpload(props) {
    //http://www.w3schools.com/html/mov_bbb.mp4
    const [file, setFile] = useState("");
    const [number, setNumber] = useState(1.0);
    const [ipaDownload,setIpaDownload]=useState("");
    const [url,setUrl]=useState("");
    useEffect(()=>{
        getNewsFile().then(res=>{
            if (res.success){
                setUrl(res.data.name);
                setFile(res.data.name);
                //setNumber(res.data.id);
            }
        })
    },[])
    //检测最新版
    useEffect(()=>{

    })
    return (<React.Fragment>
        <div className="action">
            <div>
                <Row>
                    <Col span={3}>
                        <Input onChange={(values) => {
                            // console.log(values.target.value);
                            setNumber(() => {
                                return values.target.value;//parseFloat(values.target.value);
                            })
                        }} style={{marginTop: '40px', height: '32px'}} placeholder="输入版本号"/>
                    </Col>
                    <Col span={9}>
                        <Input onChange={(values)=>{
                            setIpaDownload(()=>{
                                console.log("values",values.target.value);
                                return values.target.value;
                            })
                        }} style={{marginTop: '40px', height: '32px'}} placeholder="输入ipa的下载链接"/>
                    </Col>
                    <Col span={3}>
                        <Upload
                            action={uploadUrl}
                            onChange={(_, currentFile) => {
                                setFile(
                                    () => {
                                        //上传数据成功,将路径插入数据库中
                                       // console.log(currentFile);
                                        if (currentFile.status === 'done') {
                                            let array = currentFile.response.staticPath.split('/');
                                            //console.log(array[3])
                                            //获取当前文件路径,判断当前文件是否已被添加进入文件服务器
                                            getFile(number,mobileType).then(
                                                res => {
                                                    //数据库没有数据,则插入数据
                                                    console.log("res",res);
                                                    if (res.data === true) {
                                                        /*insertFile(number, array[3]).then(r => {
                                                            return URL.createObjectURL(currentFile.originFile);
                                                        })*/
                                                        //https://xdydyw.shasteel.cn/xdapp-service/AnonService?type=mobile&service=JPES50&method=addVersion
                                                        let obj={};
                                                        obj.q_id=number;
                                                        obj.q_name=currentFile.response.staticPath;
                                                        obj.q_type=mobileType;
                                                        obj.q_ipaDownload=ipaDownload;
                                                        //上传最新版
                                                        console.log("obj",obj);
                                                        axios.post(`https://xdydyw.shasteel.cn/xdapp-service/AnonService?type=mobile&service=JPES50&method=addVersion`,qs.stringify(obj)).then(r=>{
                                                            setUrl(URL.createObjectURL(currentFile.originFile));
                                                            if (mobileType==='ios'){
                                                                insertFile(obj.q_id, obj.q_ipaDownload,mobileType).then();
                                                            }
                                                            else {
                                                                insertFile(obj.q_id, obj.q_name,mobileType).then();
                                                            }
                                                            return  URL.createObjectURL(currentFile.originFile);
                                                           /* insertFile(obj.q_id, obj.q_name).then(r=>{
                                                                return  URL.createObjectURL(currentFile.originFile);
                                                            })*/
                                                        })
                                                    }
                                                }
                                            )
                                        }

                                    }
                                );
                            }}
                            style={{marginTop: 40,}}
                        />
                    </Col>
                    <Col>
                        <Button type={"primary"} style={{marginTop: '40px', height: '32px'}}><a href={`${url}`}>下载最新版</a></Button>
                    </Col>
                </Row>
            </div>
            <div style={{width: '100%', height: '270px'}}>
                <Player
                    playsInline
                    src={file}
                    fluid={false}
                    width={'100%'}
                    height={'100%'}
                    preload={'auto'}
                />
            </div>

        </div>
    </React.Fragment>)
}
/*
<Upload className="mt-3 mb-3"
                        accept='*'
                       action={'https://xdydyw.shasteel.cn/files/upload'}
                      //action={`http://127.0.0.1:3000/upload`}
                      onSuccess={(e)=>{
                           e.status='done';
                        console.log(e);
                       }
listType="picture"
maxCount={1}
onChange={(file) => {
    handleChange(file);
    console.log("@")
    axios.post('https://xdydyw.shasteel.cn/files/upload',file.originFileObj).then(
        r => {
            console.log("@")
            handleChange(file);
        });
}}
>
<Button icon={<UploadOutlined/>}>
Upload
</Button>
</Upload>
const fd = new FormData();
           fd.append('file', file.originFileObj);
          axios({
              url: 'https://xdydyw.shasteel.cn/files/upload',
              method:'post',
              data:fd,
          }).then(r =>{
              console.log("yes");
          });
* */